package info.mhylle.mediaplayer.utils;

import org.jboss.seam.international.LocaleSelector;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import java.util.Map;

public class LocalePhaseListener implements PhaseListener {

    public void afterPhase(PhaseEvent event) {
        String locale = getStringParameter("locale");

        if (locale != null && !"".equals(locale)) {
            System.out.println("setting locale");
            LocaleSelector.instance().setLanguage("en");
            LocaleSelector.instance().initLocale();
            LocaleSelector.instance().select();
        }
    }

    private String getStringParameter(String parameterName) {
        Map<String, String> map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return map.get(parameterName);
    }

    public void beforePhase(PhaseEvent event) {

    }

    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }
}
